package com.ryder.auth.application.interceptor;

import com.alibaba.fastjson2.JSON;
import com.ryder.auth.application.context.LoginContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;

/**
 * 描述: 登录拦截器
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025/03/05
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestUri = request.getRequestURI(); // 获取请求的 URI
        if ("/user/doLogin".equals(requestUri)) {
            return HandlerInterceptor.super.preHandle(request, response, handler);
        }
        String loginId = request.getHeader("loginId");
        LoginContextHolder.set("loginId", loginId);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        LoginContextHolder.remove();
    }
}
